<template>
  <div class="publicElevator">
    <div class="co-wrap">
      <!-- 电梯选择面板 -->
      <div class="flex flexdc flexcenter">
        <!-- 电梯查询条件 -->
        <div class="flex">
          <div class="elev-left m-r-20">
            <div class="entryName m-b-15">
              <span class="name">电梯注册代码：</span>
              <el-input v-model="param.registerCode" class="entry-input co-input" placeholder="请输入注册代码" />
            </div>
            <div class="entryName rescueCode m-b-15">
              <span class="code-name">位置码：</span>
              <el-input v-model="param.rescueCode" class="entry-input co-input" placeholder="请输入位置码" />
            </div>
          </div>
          <div class="elev-right m-l-30">
            <div class="entryName m-b-15">
              <span class="name">维保单位：</span>
              <el-input v-model="param.maintenanceUnitName" readonly class="entry-input" placeholder="请选择维保单位" />
              <el-button type="warning" class="unitbtn m-l-10" @click="handleSelectUnit(0)">选择单位</el-button>
            </div>
            <div class="entryName m-b15">
              <span class="name">使用单位：</span>
              <el-input v-model="param.useUnitName" readonly class="entry-input" placeholder="请选择使用单位" />
              <el-button type="warning" class="unitbtn m-l-10" @click="handleSelectUnit(1)">选择单位</el-button>
            </div>
          </div>
        </div>
        <div class="flex m-t-10">
          <el-button class="flex flexac m-r-20 fs14" size="small" type="warning" @click="getCoSelectList"><i class="co-i" /><span class="search1">查 询</span></el-button>
          <el-button class="co-empty flexcenter m-l-25" type="info" @click="handleEmptySearch"><i class="iconfont icon-qingkong" />清 空</el-button>
        </div>
        <!-- 电梯列表 -->
        <el-table
          id="publicElevator"
          ref="publicTable"
          class="con-table m-t-20"
          type="expand"
          style="width: 100%"
          highlight-current-row
          :data="dataList"
          :row-key="getRowKey"
          @select="handleSelect"
        >

          <el-table-column
            :reserve-selection="true"
            type="selection"
            :selectable="selectInit"
            name="www"
            align="center"
            width="55"
            height="40"
          />

          <template v-for="item in tablehead">
            <el-table-column :key="item.index" :width="item.width" align="center" :sortable="item.sortable" :show-overflow-tooltip="true" :prop="item.prop" :label="item.label" />
          </template>

          <el-table-column align="center" label="状态" width="100">
            <template slot-scope="scope">
              <span :class="`status-${scope.row.bindState} co-status`">
                {{
                  scope.row.bindState === 0 ? '未绑定' :
                  scope.row.bindState === 1 ? '已录入' :
                  scope.row.bindState === 2 ? '已申请' :
                  scope.row.bindState === 3 ? '已绑定' :
                  scope.row.bindState === 4 ? '已拒绝' : '未确认'
                }}
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-pagination
      class="co-page flex m-l-10"
      :current-page="paging.currentPage"
      :page-sizes="[4]"
      :page-size="paging.pageSize"
      :pager-count="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="paging.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <UserUnit v-model="unitVisible" @update="handleUnitUpdate" @update1="handleMaUpdate" />
  </div>
</template>

<script>

import UserUnit from '@/components/userUnit/index'
import { elevatorQueryList } from '@/api/elevator/elevator'
import storage from '@/utils/storage'
import { showNotice } from '@/utils/toolclass'
import bus from '@/utils/bus'

export default {
  name: 'PublicElevator',
  components: { UserUnit },
  props: {
    data: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      row: {},
      dataList: [],
      userList: [],
      unitVisible: false,
      paging: {
        currentPage: 1,
        pageSize: 4,
        pageSizeArr: [4, 8, 10, 14],
        total: 0
      },
      param: {
        bindState: '',
        id: '',
        registerCode: '',
        registerUnit: '',
        rescueCode: '',
        maintenanceUnitId: '',
        maintenanceUnitName: '',
        useUnitId: '',
        useUnitName: ''
      },
      tablehead: [
        {
          index: 0,
          prop: 'registerCode',
          label: '电梯注册代码',
          width: '180',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 1,
          prop: 'rescueCode',
          label: '救援代码',
          width: '100',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 2,
          prop: 'installAddress',
          label: '安装地址',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 3,
          prop: 'useUnitName',
          label: '使用单位',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 4,
          prop: 'maintenanceUnitName',
          label: '维保单位',
          sortable: false,
          fixed: true,
          isShow: true
        }
      ],
      eleList: [],
      allData: []
    }
  },
  computed: {},
  created() {},
  mounted() {
    this.userInfo = JSON.parse(storage.get('userInfo'))
    this.getCoSelectList()
    this.getList()
  },
  methods: {
    handleSelectUnit(type) {
      bus.$emit('type', type)
      this.unitVisible = true
    },
    handleUnitUpdate(row) {
      this.param.useUnitId = row.id
      this.param.useUnitName = row.name
      // console.log(row)
    },
    handleMaUpdate(row) {
      this.param.maintenanceUnitId = row.id
      this.param.maintenanceUnitName = row.name
    },
    getRowKey(row) {
      return row.id
    },
    getList() {
      // 获取所有电梯数据
      elevatorQueryList('', '', '', 1, 10000, '', '', '', '').then(res => {
        // console.log('res', res)
        if (res.code === '000000') {
          // this.allData = res.data
          bus.$on('eles', (val) => {
            // console.log(val)
            // this.$nextTick(res => {})
            if (val) {
              val.map(item => {
                // console.log(res.data)
                res.data.map(item1 => {
                  if (item === item1.id) {
                    this.$refs.publicTable.toggleRowSelection(item1, true)
                  }
                })
              })
            }
          })
        }
      })
    },
    handleSelectSave() {
      if (!this.selectioned) {
        showNotice('error', '提示', '请选择公司')
      } else {
        this.$refs.publicTable.clearSelection()
        this.$emit('update', this.row)
        this.$emit('handleRefresh')
        this.$emit('handleCloseDialog')
      }
    },
    handleSizeChange(val) {
      this.paging.pageSize = val
      this.getCoSelectList()
    },
    handleCurrentChange(val) {
      this.paging.currentPage = val
      this.getCoSelectList()
    },
    selectInit(row, index) {
      if (row.bindState !== 3) {
        return false
      } else {
        return true
      }
    },
    handleSelect(rows, row) {
      this.$emit('update', rows)
    },

    // 获取电梯列表
    getCoSelectList() {
      elevatorQueryList(this.param.bindState, this.param.id, this.param.maintenanceUnitId, this.paging.currentPage, this.paging.pageSize, this.param.registerCode, this.param.registerUnit, this.param.rescueCode, this.param.useUnitId).then(res => {
        // console.log('res', res)
        if (res.code === '000000') {
          this.dataList = res.data
          this.paging = {
            pageSize: res.pager.pageSize,
            currentPage: res.pager.pageIndex,
            total: res.pager.totalSize
          }
        } else {
          // showNotice('error', '提示', res.msg)
        }
      })
    },
    handleEmptySearch() {
      this.param.registerCode = ''
      this.param.rescueCode = ''
      this.param.maintenanceUnitId = ''
      this.param.maintenanceUnitName = ''
      this.param.useUnitId = ''
      this.param.useUnitName = ''
      this.getCoSelectList()
    }
  }

}
</script>

<style lang="scss">
.publicElevator {
  .el-icon-success:before {
    margin-right: 4px;
  }
  .co-wrap {
    // width: 600px;
    height: 450px;
    background: #ffffff;
    position: relative;
    .co-status {
      font-size: 16px;
      font-weight: 700;
    }
    .status-0 { color: red; }
    .status-1 { color: darkgoldenrod; }
    .status-2 { color: blue; }
    .status-3 { color: green; }
    .status-4 {}
    .status-5 {}
    .rescueCode {
      margin-left: 42px;
    }
    .co-page {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
    }
  }
}

</style>
